package com.ruoyi.web.controller.sxt;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.sxt.domain.SxtCaidan;
import com.ruoyi.sxt.service.ISxtCaidanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜单权限Controller
 * 
 * @author ruoyi
 * @date 2025-07-02
 */
@Controller
@RequestMapping("/sxt/caidan")
public class SxtCaidanController extends BaseController
{
    private String prefix = "sxt/caidan";

    @Autowired
    private ISxtCaidanService sxtCaidanService;

    @RequiresPermissions("sxt:caidan:view")
    @GetMapping()
    public String caidan()
    {
        return prefix + "/caidan";
    }

    /**
     * 查询菜单权限列表
     */
    @RequiresPermissions("sxt:caidan:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SxtCaidan> list(SxtCaidan sxtCaidan)
    {
        List<SxtCaidan> list = sxtCaidanService.selectSxtCaidanList(sxtCaidan);
        return list;
    }

    /**
     * 导出菜单权限列表
     */
    @RequiresPermissions("sxt:caidan:export")
    @Log(title = "菜单权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SxtCaidan sxtCaidan)
    {
        List<SxtCaidan> list = sxtCaidanService.selectSxtCaidanList(sxtCaidan);
        ExcelUtil<SxtCaidan> util = new ExcelUtil<SxtCaidan>(SxtCaidan.class);
        return util.exportExcel(list, "菜单权限数据");
    }

    /**
     * 新增菜单权限
     */
    @RequiresPermissions("sxt:caidan:add")
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        SxtCaidan menu = null;
        if (0L != parentId)
        {
            menu = sxtCaidanService.selectSxtCaidanByMenuId(parentId);
        }
        else
        {
            menu = new SxtCaidan();
            menu.setMenuId(0L);
            menu.setMenuName("主目录");
        }
        mmap.put("menu", menu);
        return prefix + "/add";
    }

    /**
     * 新增保存菜单权限
     */
    @RequiresPermissions("sxt:caidan:add")
    @Log(title = "菜单权限", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SxtCaidan sxtCaidan)
    {
        return toAjax(sxtCaidanService.insertSxtCaidan(sxtCaidan));
    }

    /**
     * 修改菜单权限
     */
    @RequiresPermissions("sxt:caidan:edit")
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        SxtCaidan sxtCaidan = sxtCaidanService.selectSxtCaidanByMenuId(menuId);
        if(sxtCaidan.getParentId() > 0){
            sxtCaidan.setParentName(sxtCaidanService.selectSxtCaidanByMenuId(sxtCaidan.getParentId()).getMenuName());
        }else{
            sxtCaidan.setParentName(null);
        }
        mmap.put("menu", sxtCaidan);
        return prefix + "/edit";
    }

    /**
     * 修改保存菜单权限
     */
    @RequiresPermissions("sxt:caidan:edit")
    @Log(title = "菜单权限", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SxtCaidan sxtCaidan)
    {
        return toAjax(sxtCaidanService.updateSxtCaidan(sxtCaidan));
    }

    /**
     * 删除菜单权限
     */
    @RequiresPermissions("sxt:caidan:remove")
    @Log(title = "菜单权限", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sxtCaidanService.deleteSxtCaidanByMenuIds(ids));
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(SxtJuese sxtJuese)
    {
        List<Ztree> ztrees = sxtCaidanService.menuTreeData(sxtJuese);
        return ztrees;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        mmap.put("menu", sxtCaidanService.selectSxtCaidanByMenuId(menuId));
        return prefix + "/tree";
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Ztree> menuTreeData()
    {
        List<Ztree> ztrees = sxtCaidanService.menuTreeData();
        return ztrees;
    }
}
